/*
 * ty_memcached.cpp
 *
 *  Created on: 2013. 6. 25.
 *      Author: the2
 */


#include "../include/ty_memcached.h"


namespace tylenol { namespace memcached {


bool CMemcached::Init(const char *address)
{
	memcached_return_t rc;
	pMemc = memcached_create( NULL );
	pServer = memcached_servers_parse( address );

	rc = memcached_server_push( pMemc, pServer );
	if( rc != MEMCACHED_SUCCESS )
	{
		return false;
	}

	if( pServer->number_of_hosts > 1 )
	{
		memcached_behavior_set( pMemc, MEMCACHED_BEHAVIOR_KETAMA_HASH, 1 );
	}

	return true;
}

bool CMemcached::Release()
{
	memcached_server_free( pServer );
	memcached_server_list_free( pServer );
	memcached_free( pMemc );

	pServer = NULL;
	pMemc = NULL;

	return true;
}

memcached_return_t CMemcached::Set( const char *key, const char *value, const size_t size, time_t expiration )
{
	return memcached_set( pMemc, key, strlen(key), value, size, expiration, 0 );
}

char *CMemcached::Get( const char *key, size_t *valuelen, uint32_t *flags, memcached_return_t *error )
{
	return memcached_get( pMemc, key, strlen(key), valuelen, flags, error );
}

memcached_return_t CMemcached::MGet( const char* const *keys, const size_t *key_length, size_t number_of_keys )
{
	return memcached_mget( pMemc, keys, key_length, number_of_keys );
}

char* CMemcached::Fetch( char *return_key, size_t *return_key_length, size_t *value_length, uint32_t *flags, memcached_return_t *error )
{
	return memcached_fetch( pMemc, return_key, return_key_length, value_length, flags, error);
}

memcached_return_t CMemcached::Delete( const char *key, size_t key_length, time_t expiration )
{
	return memcached_delete( pMemc, key, key_length, expiration );
}

const char* CMemcached::GetErrorString( memcached_return_t rc )
{
	return memcached_strerror( pMemc, rc );
}

memcached_return_t CMemcached::FlushAll()
{
	return memcached_flush(this->pMemc, 0);
}

} /*memcached*/ } /*tylenol*/

